package com.question.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.paper.dao.SubtopicsRepository;
import com.paper.entity.Subtopics;
import com.question.dao.BlankRepository;
import com.question.entity.Blank;
import com.question.entity.Multiple;
@Service
@Transactional
public class BlankService implements IBlankService {
	@Autowired
	private BlankRepository blankDao;
	@Autowired
	private SubtopicsRepository subtopicsDao;
	@Autowired
	private SubjectRepository subjectDao;
	
	@Override
	public void saveOne(Blank blank) {
		// TODO Auto-generated method stub
		blankDao.save(blank);
	}

	@Override
	public List<Blank> findAll() {
		// TODO Auto-generated method stub
		return (List<Blank>) blankDao.findAll();
	}

	@Override
	public TablePage<Blank> findPage(Specification<Blank> spec, Pageable pageable) {
		Page<Blank> page = blankDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}

	@Override
	public void saveOrUpdate(Blank blank) {
		// TODO Auto-generated method stub
		blankDao.save(blank);
	}

	@Override
	public boolean isUsed(Long id) {
		// TODO Auto-generated method stub
		List<Subtopics> list = subtopicsDao.findByBlankId(id);
		if(list.size()>0)return true;
		return false;
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		blankDao.delete(id);
	}

	@Override
	public Blank findOne(Long id) {
		// TODO Auto-generated method stub
		return blankDao.findOne(id);
	}

	@Override
	public boolean isNumExist(Blank blank) {
		// TODO Auto-generated method stub
		Blank s = blankDao.findByNum(blank.getNum());
		if(null!=s&&s.getId()!=blank.getId())return true;
		return false;
	}

	@Override
	public boolean isUsing(Long id) {
		// TODO Auto-generated method stub
		if(subtopicsDao.findByBlankId(id).size()>0)
			return true;
		return false;
	}

}
